package com.pospi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pospi.dto.GoodsDto;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.cash.PointActivity;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.util.DoubleSave;

import java.util.List;

/**
 * Created by acer on 2018/11/13.
 */

public class ShoppingAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsBeens;

    public ShoppingAdapter(Context context, List<GoodsDto> goodsBeens) {
        this.context = context;
        this.goodsBeens = goodsBeens;
    }
    //清除数据
    public void delete() {
        goodsBeens.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return goodsBeens.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsBeens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.lvsale_item, null);
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.momey = (TextView) convertView.findViewById(R.id.tv_money);
            holder.price = (TextView) convertView.findViewById(R.id.tv_num_price);
            holder.num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.tv_bh = (TextView) convertView.findViewById(R.id.tv_bh);
            holder.ll_bg = (LinearLayout) convertView.findViewById(R.id.ll_bg);
            holder.modified = (TextView) convertView.findViewById(R.id.text_modified);
            holder.dzc = convertView.findViewById(R.id.tv_dzc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final GoodsDto goodsBean = goodsBeens.get(position);
        if (goodsBean.getProType().equals(Constant.PRO_ZP)) {
            holder.name.setText("[赠品]" + goodsBean.getName());
        } else {
            holder.name.setText(goodsBean.getName());
        }
//            holder.name.setText(goodsBean.getName().trim());
        holder.price.setText(String.valueOf(DoubleSave.doubleSaveTwo(goodsBean.getPrice())) + "/" + goodsBean.getUnit());
        holder.momey.setText(String.valueOf(DoubleSave.doubleSaveTwo(goodsBean.getNum() * goodsBean.getPrice() - goodsBean.getDiscount())));
        holder.tv_bh.setText(goodsBean.getCode());
        holder.num.setText(String.valueOf(goodsBean.getNum()));
        if (position == goodsBeens.size() - 1) {
            holder.ll_bg.setBackgroundColor(context.getResources().getColor(R.color.WhiteSmoke));
        } else {
            holder.ll_bg.setBackgroundColor(context.getResources().getColor(R.color.white));
        }

        final double num = goodsBean.getNum();
//            holder.jian.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (num > 1) {
//                        goodsBean.setNum(DoubleSave.doubleSaveTwo(num - 1));
//                        adapter.notifyDataSetChanged();
//                        setScreenData();
//                    }
//                }
//            });
//            holder.add.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    goodsBean.setNum(DoubleSave.doubleSaveTwo(num + 1));
//                    adapter.notifyDataSetChanged();
//                    setScreenData();
//                }
//            });
        return convertView;
    }
    class ViewHolder {
        TextView name;
        TextView price;
        TextView momey;
        TextView num;
        LinearLayout ll_bg;
        TextView tv_bh;
        TextView modified;
        TextView dzc;
    }
}
